package com.wd.common.viewmodel

import android.annotation.SuppressLint
import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.wd.common.base.BaseViewModel
import com.wd.common.bean.AllQuickSearchBean
import com.wd.common.bean.BanneBean
import com.wd.common.bean.RimRecommendBean
import com.wd.common.bean.BuyCarBean
import com.wd.common.bean.ContrastBean
import com.wd.common.bean.FollowBean
import com.wd.common.bean.HomeRecOneBean
import com.wd.common.bean.ManyBean
import com.wd.common.bean.NearbyChargeStationBean
import com.wd.common.bean.QuestionBean
import com.wd.common.bean.QuestionXqBean
import com.wd.common.bean.QuickSearchBean
import com.wd.common.bean.SendRimBean
import com.wd.common.bean.TalkBean
import com.wd.common.bean.TalkXqBean
import com.wd.common.bean.TiwenBean
import com.wd.common.bean.UserInfoBean
import com.wd.common.bean.ZhuanRimBean
import com.wd.common.http.HttpUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

/**
 * 作者:孙殿彭 by mac on2023/9/11 11:01
 * 类注释:
 */
class MyViewModel : BaseViewModel() {
    //首页·轮播图
    //轮播
    val banner: MutableLiveData<BanneBean> by lazy {
        MutableLiveData<BanneBean>()
    }
    val Recom: MutableLiveData<RimRecommendBean> by lazy {
        MutableLiveData<RimRecommendBean>()
    }
    val New: MutableLiveData<RimRecommendBean> by lazy {
        MutableLiveData<RimRecommendBean>()
    }

    val Interest: MutableLiveData<RimRecommendBean> by lazy {
        MutableLiveData<RimRecommendBean>()
    }

    val RimZhuan: MutableLiveData<ZhuanRimBean> by lazy {
        MutableLiveData<ZhuanRimBean>()
    }
//发送贴子
    val SendRim:MutableLiveData<SendRimBean> by lazy {
        MutableLiveData<SendRimBean>()
    }
    //话题
   val talk:MutableLiveData<TalkBean> by lazy {
       MutableLiveData<TalkBean>()
   }

    //根据话题id查询话题详情
    val talkxq:MutableLiveData<TalkXqBean> by lazy {
        MutableLiveData<TalkXqBean>()
    }

    val question:MutableLiveData<QuestionBean> by lazy {
        MutableLiveData<QuestionBean>()
    }
    val questionXq:MutableLiveData<QuestionXqBean> by lazy {
        MutableLiveData<QuestionXqBean>()
    }

    val tiwen:MutableLiveData<TiwenBean> by lazy {
        MutableLiveData<TiwenBean>()
    }

    fun getBanner() {
        HttpUtils.getInstance().apiService.Banne().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                banner.value = it
            }, {
                Log.i("adadada", "getBanner: " + it.message.toString())
            })
    }

    //问答详情
    fun getQuestionXq(problemId:String){
        HttpUtils.getInstance().apiService.getQuestionXq(problemId).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                questionXq.value=it
            }
    }

    //问答
    fun getQuestion(page:String,size:String){
        HttpUtils.getInstance().apiService.getQuestion(page, size)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                question.value=it
            }
    }

    fun getTalkXq(hotId:String){//根据话题id查询话题详情
        HttpUtils.getInstance().apiService.getTalkXq(hotId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                talkxq.value=it
            }
    }

    fun getTalk(){//热门话题
        HttpUtils.getInstance().apiService.getTalk().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                talk.value=it
            }
    }

    fun getSendRim(//发送帖子
        title: String,
        columnId: Int,
        type: Int,
        content: String,
        publishLocation: String,
        list:List<File>){
        var map: List<MultipartBody.Part> = mutableListOf()
        if(list.isNotEmpty()){
            map = list.map {
                val create = it.asRequestBody("multipart/form-data".toMediaTypeOrNull())
                MultipartBody.Part.createFormData("images", it.name, create)
            }
        }
        HttpUtils.getInstance().apiService.getSendRim(title,columnId,type,content,publishLocation,map)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                SendRim.value=it
            }
    }


    fun getTiwen(//发送帖子
        content: String,
        publishLocation: String,
        list:List<File>){
        var map: List<MultipartBody.Part> = mutableListOf()
        if(list.isNotEmpty()){
            map = list.map {
                val create = it.asRequestBody("multipart/form-data".toMediaTypeOrNull())
                MultipartBody.Part.createFormData("images", it.name, create)
            }
        }
        HttpUtils.getInstance().apiService.getTiwen(content,publishLocation,map)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                tiwen.value=it
            }
    }

    //金刚区
    val recone: MutableLiveData<HomeRecOneBean> by lazy {
        MutableLiveData<HomeRecOneBean>()
    }

    fun getHomeRecOne() {
        HttpUtils.getInstance().apiService.RecOne().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                recone.value = it
            }, {
                Log.i("adadada", "getRecOne: " + it.message.toString())
            })
    }


    fun getRecom(page: Int, size: Int) {
        HttpUtils.getInstance().apiService.getRecom(page, size)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                Recom.value = it
            }
    }

    fun getNew(page: Int, size: Int) {
        HttpUtils.getInstance().apiService.getNew(page, size)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                New.value = it
            }
    }


    fun getInterest(page: Int, size: Int) {
        HttpUtils.getInstance().apiService.getInterest(page, size)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                Interest.value = it
            }
    }

    //买车·二手车·列表
    val buy_oldcarListLiveData: MutableLiveData<BuyCarBean.BuyOldCarListBean> by lazy {
        MutableLiveData<BuyCarBean.BuyOldCarListBean>()
    }

    fun buy_oldcarList(page: Int, size: Int) {
        HttpUtils.getInstance().apiService.Buy_oldcar_list(page, size)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                buy_oldcarListLiveData.value = it
            }, {
                Log.i("TAG", "buy_oldcarListLiveData: " + it.message.toString())
            })
    }

    //买车·新车·列表
    val buy_newcarListLiveData: MutableLiveData<BuyCarBean.BuyCarListBean> by lazy {
        MutableLiveData<BuyCarBean.BuyCarListBean>()
    }

    fun getbuy_newcarlist() {
        HttpUtils.getInstance().apiService.Buycar_querylist()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                buy_newcarListLiveData.value = it
            }, {
                Log.i("TAG", "buy_newcarListLiveData: " + it.message.toString())
            })
    }

    //买车·新车·选择车系
    val buy_newcarClooseListLiveData: MutableLiveData<BuyCarBean.BuyNewCar_ChooseList> by lazy {
        MutableLiveData<BuyCarBean.BuyNewCar_ChooseList>()
    }

    fun buy_newcarClooseList(brandId: Int, page: Int, size: Int) {
        HttpUtils.getInstance().apiService.Buy_newcar_cloose(brandId, page, size)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                buy_newcarClooseListLiveData.value = it
            }, {
                Log.i("TAG", "buy_newcarClooseListLiveData: " + it.message.toString())
            })
    }

    //买车·新车·详情
    val buy_newcarDetailsLiveData: MutableLiveData<BuyCarBean.BuyNewCar_CarDetails> by lazy {
        MutableLiveData<BuyCarBean.BuyNewCar_CarDetails>()
    }
    fun buy_newcarDetails(categoryId:Int){
        HttpUtils.getInstance().apiService.Buy_newcar_details(categoryId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe ({
                buy_newcarDetailsLiveData.value=it
            },{
                Log.i("TAG", "buy_newcarDetailsLiveData: " + it.message.toString())
            })
    }

    //买车·二手车·搜索
    val buy_oldcarClooseListLiveData: MutableLiveData<BuyCarBean.Buy_OldCar_clooselist> by lazy {
        MutableLiveData<BuyCarBean.Buy_OldCar_clooselist>()
    }
    fun buy_oldcarClooseList(keyword:String,page: Int,size: Int){
        HttpUtils.getInstance().apiService.Buy_oldcar_clooselist(keyword, page, size)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe ({
                buy_oldcarClooseListLiveData.value=it
            },{
                Log.i("TAG", "buy_oldcarClooseListLiveData: " + it.message.toString())
            })
    }

    //买车·新车·详情·外观内饰
    val buy_oldcarwaineiLiveData: MutableLiveData<BuyCarBean.Buy_NewCar_waineiBean> by lazy {
        MutableLiveData<BuyCarBean.Buy_NewCar_waineiBean>()
    }
    fun buy_oldcarwainei(categoryId:Int,type: Int){
        HttpUtils.getInstance().apiService.Buy_newcar_wainei(categoryId,type)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe ({
                buy_oldcarwaineiLiveData.value=it
            },{
                Log.i("TAG", "buy_oldcarwaineiLiveData: " + it.message.toString())
            })
    }

    //买车·新车·销量
    val buy_newcar_xiaoliangLiveData: MutableLiveData<BuyCarBean.Buy_newcar_XiaoliangBean> by lazy {
        MutableLiveData<BuyCarBean.Buy_newcar_XiaoliangBean>()
    }
    fun buy_newcar_xiaoliang(gradeId:Int){
        HttpUtils.getInstance().apiService.Buy_newcar_xiaoliang(gradeId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe ({
                buy_newcar_xiaoliangLiveData.value=it
            },{
                Log.i("TAG", "buy_newcar_xiaoliangLiveData: " + it.message.toString())
            })
    }

    //买车·二手车·详情
    val buy_oldcar_detailsLiveData: MutableLiveData<BuyCarBean.Buy_oldcar_detailsBean> by lazy {
        MutableLiveData<BuyCarBean.Buy_oldcar_detailsBean>()
    }
    fun buy_oldcar_details(carId:Int){
        HttpUtils.getInstance().apiService.Buy_oldcar_details(carId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe ({
                buy_oldcar_detailsLiveData.value=it!!
            },{
                Log.i("TAG", "buy_oldcar_detailsLiveData: " + it.message.toString())
            })
    }

    //买车·二手车·收藏
    val buy_oldcar_detailsshoucangLiveData: MutableLiveData<BuyCarBean.BuyOldCarShouCangBean> by lazy {
        MutableLiveData<BuyCarBean.BuyOldCarShouCangBean>()
    }
    fun buy_oldcar_detailsshoucang(secondCarId:Int){
        HttpUtils.getInstance().apiService.Buy_oldcar_detailsshoucang(secondCarId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                buy_oldcar_detailsshoucangLiveData.value=it
            },{
                Log.i("TAG", "buy_oldcar_detailsshoucangLiveData: " + it.message.toString())
            })
    }
    //买车·二手车·取消收藏
    val buy_oldcar_deteleshoucangLiveData: MutableLiveData<BuyCarBean.BuyOldCarDeteleShouCangBean> by lazy {
        MutableLiveData<BuyCarBean.BuyOldCarDeteleShouCangBean>()
    }
    fun buy_oldcar_deteleshoucang(secondCarId:Int){
        HttpUtils.getInstance().apiService.Buy_oldcar_deteleshoucang(secondCarId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                buy_oldcar_deteleshoucangLiveData.value=it
            },{
                Log.i("TAG", "buy_oldcar_deteleshoucangLiveData: " + it.message.toString())
            })
    }

    //查询收藏列表
    val query_buyoldcardetailsshoucangListLiveData: MutableLiveData<BuyCarBean.QueryBuyOldCarShouCangListBean> by lazy {
        MutableLiveData<BuyCarBean.QueryBuyOldCarShouCangListBean>()
    }
    fun query_buyoldcardetailsshoucangList(page: Int,size: Int){
        HttpUtils.getInstance().apiService.QueryBuyOldCarShouCangList(page, size)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                query_buyoldcardetailsshoucangListLiveData.value=it
            },{
                Log.i("TAG", "query_buyoldcardetailsshoucangListLiveData: " + it.message.toString())
            })
    }
    //买车·二手车·询价
    val BuyoldcarInquiryLiveData: MutableLiveData<BuyCarBean.BuyOldCarInquiryBean> by lazy {
        MutableLiveData<BuyCarBean.BuyOldCarInquiryBean>()
    }

    fun BuyoldcarInquiry( carId: Int, name: String, phone: String, city: String) {
        val requestData = BuyCarBean.RequestData(carId, name, phone, city)
        HttpUtils.apiService.Buy_oldcar_Inquiry(requestData)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                BuyoldcarInquiryLiveData.value = it
            },{
                Log.i("TAG", "BuyoldcarInquiryLiveData: " + it.message.toString())
            })
    }
    //普通登录
    val userLoginData: MutableLiveData<UserInfoBean> by lazy {
        MutableLiveData<UserInfoBean>()
    }

    fun postUserLogin(userName: String, passwd: String) {
        var map = HashMap<String, String>()
        map["userName"] = userName
        map["passwd"] = passwd
        HttpUtils.apiService.userLogin(map)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                userLoginData.value = it
            }, {
                it.message?.let { it1 -> Log.e("error", it1) }
            })
    }


    //首页多条目-车圈列表
    val manyrecmu: MutableLiveData<ManyBean> by lazy {
        MutableLiveData<ManyBean>()
    }

    fun ManyRec(int: Int, int1: Int) {
        HttpUtils.getInstance().apiService.ManyRec(int, int1).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread()).subscribe({
                manyrecmu.postValue(it)
            })
    }
    //看车快速查询 因接口无法请求所以使用假数据来获取
    val quickSearch: MutableLiveData<QuickSearchBean> by lazy {
        MutableLiveData<QuickSearchBean>()
    }
    @SuppressLint("CheckResult")
    fun lookQuickSearch(price:String, grade:String, rank:String, page: Int, size: Int){
        val map = HashMap<String,String>()
        map["price"] = price
        map["grade"] = grade
        map["rank"] = rank
        HttpUtils.apiService.lookQuickSearch(map, page, size)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                quickSearch.value = it
            },{
                it.message?.let { it1 -> Log.e("lookErr", it1) }
            })
    }
    fun multiConditionQuery(page: Int, size: Int,map : HashMap<String,String>){
        HttpUtils.apiService.multiConditionQuery(page, size, map)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                quickSearch.value = it
            },{
                it.message?.let { it1 -> Log.e("lookErr", it1) }
            })
    }
    val allQuickSear: MutableLiveData<AllQuickSearchBean> by lazy {
        MutableLiveData<AllQuickSearchBean>()
    }
    fun findAllCondition(){
        HttpUtils.apiService.findAllCondition()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                allQuickSear.value = it
            },{
                it.message?.let { it1 -> Log.e("lookErr", it1) }
            })
    }

    fun getZhuan(){
        HttpUtils.getInstance().apiService.getZhuan()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                RimZhuan.value=it
            }
    }
    val meFollow: MutableLiveData<FollowBean> by lazy {
        MutableLiveData<FollowBean>()
    }
    @SuppressLint("CheckResult")
    fun followList(){
        HttpUtils.apiService.meFollow(SPUtils.getInstance().getInt("userId"),SPUtils.getInstance().getString("token"),1,10)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                meFollow.value = it
            },{
                it.message?.let { it1 -> Log.e("lookErr", it1) }
            })
    }
    val nearChargeStation: MutableLiveData<NearbyChargeStationBean> by lazy {
        MutableLiveData<NearbyChargeStationBean>()
    }

    fun nearChargeStation(location:String){
        HttpUtils.apiService.nearbyChargeStation(location)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                nearChargeStation.value = it
            },{
                it.message?.let { it1 -> Log.e("lookErr", it1) }
            })
    }

    val findCarParameter: MutableLiveData<ContrastBean> by lazy {
        MutableLiveData<ContrastBean>()
    }
    fun findCarParameter(ids : String){
        HttpUtils.apiService.findCarParameter(ids)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                findCarParameter.value = it
            },{
                it.message?.let { it1 -> Log.e("lookErr", it1+"ids:$ids") }
            })
    }


}